﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;

namespace CRS.Web.BackStageManagement
{
    public partial class RoleEdit : VcPage
    {
        private BaseDataManager m_help = new BaseDataManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            if (!IsPostBack)
            {
                if (Request.QueryString["Id"] != null)
                {
                    labelId.Text = Request.QueryString["Id"].Trim();
                    Role currentRole;
                    IList objlist = m_help.GetObjectList(new Role().GetType().Name, "Id", labelId.Text.Trim());
                    if (objlist != null && objlist.Count > 0)
                    {
                        currentRole = (Role)objlist[0];
                        ZLTextBox_Name.Text = currentRole.Name;
                        ZLTextBox_Alias.Text = currentRole.Alias;
                        ZLTextBox_Memo.Text = currentRole.Memo;
                    }
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("RoleList.aspx", false);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            bool access = false;
            string strName = ZLTextBox_Name.Text.Trim();
            string strAlias = ZLTextBox_Alias.Text.Trim();
            string strMemo = ZLTextBox_Memo.Text.Trim();

            if (labelId.Text.Trim().Length > 0 && labelId.Text.Trim().Length == 36)
            {
                IList<Role> list =
                    m_help.Session.CreateQuery("From Role R where R.Alias ='" + strAlias +  "' and R.Id != '" + labelId.Text.Trim() + "'").List<Role>();
                if(list != null && list.Count > 0)
                {
                    Alert.MessageBox("别名已存在！");
                    return;
                }
                IList<Role> list2 =
                   m_help.Session.CreateQuery("From Role R where R.Name ='" + strName + "' and R.Id != '" + labelId.Text.Trim() + "'").List<Role>();
                if (list2 != null && list2.Count > 0)
                {
                    Alert.MessageBox("名称已存在！");
                    return;
                }
                 IList objList = m_help.GetObjectList(new Role().GetType().Name, "Id", labelId.Text.Trim());
                 if (objList != null && objList.Count > 0)
                 {
                     var currentRole = (Role)objList[0];
                     currentRole.Name = strName;
                     currentRole.Memo = strMemo;
                     currentRole.Alias = strAlias;
                     access = m_help.SaveOrUpdateObj(currentRole);
                 }
                 
            }
            else
            {
                IList objList = m_help.GetObjectList(new Role().GetType().Name, "Alias", strAlias);
                if(objList != null && objList.Count > 0)
                {
                    Alert.MessageBox("角色别名已存在！");
                    return;
                }
                IList objList2 = m_help.GetObjectList(new Role().GetType().Name, "Name", strAlias);
                if (objList2 != null && objList.Count > 0)
                {
                    Alert.MessageBox("角色名称已存在！");
                    return;
                }
                var currentRole = new Role();
                currentRole.Name = strName;
                currentRole.Alias = strAlias;
                currentRole.Memo = strMemo;
                access = m_help.SaveOrUpdateObj(currentRole);
            }
            if (access)
            {
                Alert.MessageBox("保存成功!");
            }
        }
    }
}
